﻿///<reference path='jquery-1.4.4-vsdoc.js'/>
$(function() {
    ///读取字典列表
    CreateDataGrid();
    ///验证
    var userForm = $("#userForm");
    var tbs = userForm.children("table").children("tbody").children("tr").children("td");
    userForm.validate({
        rules: {
            catagoryTxt: {
                required: true,
                minlength: 2,
                remote: {
                    url: "../Admin/VerifyCatagoryName",     //后台处理程序 
                    type: "post",               //数据发送方式 
                    dataType: "json"           //接受数据格式    
                }
            }
        }
    });
});
///加载数据到datagrid
function CreateDataGrid() {
    $('#w').window('close');
    var result = $("#result");
    var w = result.parent("div").width()*0.98;

    var lwidth = w * 0.3;
    var rwidth = w * 0.2;
    var test = $('#test');
    test.datagrid({
        title: '修改、删除字典项信息',
        iconCls: 'icon-save',
        width: w,
        height: 350,
        nowrap: false,
        striped: true,
        collapsible: true,
        url: '../Admin/GetItems',
        loadMsg: "正在加载数据......",
        columns: [[
			        { title: '字典项', colspan: 3 },
					{ field: 'opt', title: '操作', width: rwidth, align: 'center', rowspan: 1,
					    formatter: function(value, rec) {
					        return '<span style=" cursor: pointer;"><a  href="javascript:void(0)" onclick="update(this)">修改</a> <a  href="javascript:void(0)"  onclick="deleteobj(this)"> 删除</a></span>';
					    }
					}
				], [
					{ field: 'itemid', title: '', width: 0 },
					{ field: 'catagrory', title: '所属类别', width: lwidth, rowspan: 1 },
					{ field: 'itemname', title: '字典名称', width: lwidth, rowspan: 1 }

				]],
        pagination: true,
        rownumbers: false
    });
    test.datagrid('hideColumn', 'itemid');
}
function update(t) {
   
    var selt = $(t);
    var td = selt.parents("td");
    var tds = td.siblings("td[field=itemid]");
    var id = tds.children("div").text();
    var tabResult = $("#tabResult");
    tabResult.html("<div id='loading'></div>");
    $.ajax({
    url: '../Admin/LoadHtmlTable_Edit',
        type: 'Post',
        data: { id: id },
        timeout: 20000,
        error: function(data) {
            tabResult.html("加载数据失败，请稍候再试！");
        },
        success: function(data) {
            tabResult.html(data);
        }
    });
    var w = $('#w');
    w.parent().bgiframe();
    w.window({
        title: '修改信息',
        modal: true,
        shadow: true,
        closed: false,
        opacity: true
    });
}
function EnsureUpdate() {
    if (!confirm("您确定修改以上信息吗？"))
        return;
    var tabResult = $("#tabResult");
    var id = tabResult.children("#editID").text();
    var tab = tabResult.children("table");
    var tds = tab.children("tbody").children("tr").children("td");
    var catagory = tds.children("#dictid_0").val();
    var itemName = tds.children("#itemname_0").val();
    if (itemName=="") {
        var errMsg = tab.siblings("#errMsg");
        errMsg.html("项名称不能为空！");
        return;
    }
    $.ajax({
        url: '../Admin//EnsureUpdate',
        type: 'Post',
        data: {
            id: id,
            catagory: catagory,
            itemName: itemName
        },
        timeout: 20000,
        error: function(data) {
            alert("发生未知异常，修改失败，请您稍候再试！");
        },
        success: function(data) {
        if (data == 1) {
            $('#w').window('close');
            $('#test').datagrid('reload');
          
            } else {
                var errMsg = tab.siblings("#errMsg");
                errMsg.html(data);
            }
        }
    });
    
}
function deleteobj(t) {
    if (!confirm("您真的要删除项吗？"))
        return;
    var selt = $(t);
    var td = selt.parents("td");
    var tds = td.siblings("td[field=itemid]");
    var id = tds.children("div").text();
    $.ajax({
         url: '../Admin/DelteItem',
        type: 'Post',
        data: { id: id },
        timeout: 20000,
        error: function(data) {
            alert("发生未知异常，删除失败，请您稍候再试！");
        },
        success: function(data) {
            if (data == 1) {
                $('#w').window('close');
                $('#test').datagrid('reload');
            } else {
                alert("发生未知异常，删除失败，请您稍候再试！");
            }
        }
    });
}
//添加字典项
function Insert(t) {
    var userForm = $("#userForm");
    var tbs = userForm.children("table").children("tbody").children("tr").children("td");
    var cat = tbs.children("#ddlCatagory").val();
    var item = tbs.children("#itemName").val();
    var err = $(t).siblings("span");
    var errmsg = "";
    if (cat == "")
        errmsg = errmsg + "请选择所属类型！";
    if (item == "") {
        errmsg = errmsg + "项名称不能为空！";
    } else {
    if (item.length < 2)
        errmsg = errmsg + "项名称长度不能小于2！"; ;
    }
    if (errmsg != "") {
        err.html(errmsg);

    } else {
         err.html("");
        $.ajax({
            url: '../Admin/AddItem',
            type: 'Post',
            data: { cat: cat, item: item },
            timeout: 20000,
            error: function(data) {
                alert("发生未知意外，服务器无响应，添加失败，请您稍后再试！");
            },
            success: function(data) {
                if (data == 1) {
                    $('#test').datagrid('reload');
                } else {
                err.html(data);
                }
            }
        });
    }

}
